//
//  JournalViewController.m
//  BSLCar
//
//  Created by dlios on 15/5/22.
//  Copyright (c) 2015年 lyz. All rights reserved.
//

#import "JournalViewController.h"
#import "XDKAirMenuController.h"

@interface JournalViewController ()
@end

@implementation JournalViewController

- (void)dealloc
{
    [_dataArray release];
    [_scroll release];
    [_tempStr release];
    [super dealloc];
}

- (void)barButtonAction:(UIBarButtonItem *)barButton {
    if ([[XDKAirMenuController sharedMenu] isMenuOpened]) {
        
        [[XDKAirMenuController sharedMenu] closeMenuAnimated];
    } else {
        [[XDKAirMenuController sharedMenu] openMenuAnimated];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
    item.title = @"返回";
    self.navigationItem.backBarButtonItem = item;
    [item release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(-5, 0, 25, 23);
    [button setImage:[UIImage imageNamed:@"iconfont-caidan.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(barButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] autorelease] initWithCustomView:button];

    
    self.navigationItem.title = @"汽车杂志";
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor redColor];
    activity.center = self.view.center;
    [self.view addSubview:activity];
    [activity startAnimating];
    [self getParserResult];
    [super viewDidLoad];
    [self createTable];
    [self.navigationItem.rightBarButtonItem release];
    [activity release];
}
- (void)createTable
{
    UITableView *mtableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    mtableview.delegate = self;
    mtableview.dataSource = self;
    mtableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *mview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 190)];
    mview.backgroundColor = [UIColor whiteColor];
    self.scroll = [[CycleScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 190) animationDuration:3];
    
    NSMutableArray *viewArray  = [NSMutableArray array];
    NSArray *picUrlArray = [JournalURLRequst getParserResult];
    for (int i = 0; i < picUrlArray.count - 1; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.scroll.bounds.size.height)];
        NSURL *url = [NSURL URLWithString:[picUrlArray objectAtIndex:i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Bei"]];
//        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [viewArray addObject:imageView];
        [imageView release];
    }
    self.scroll.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
    self.scroll.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewArray[pageIndex];
    };
    self.scroll.totalPagesCount = ^NSInteger(void){
        return picUrlArray.count - 1;
    };
    self.scroll.TapActionBlock = ^(NSInteger pageIndex){
        //        NSLog(@"点击了第%d个",pageIndex);
        NSArray *array = [picUrlArray lastObject];
        NSArray *secA = [array lastObject];
        FPScrollViewController *thvc = [[FPScrollViewController alloc] init];
        thvc.url = [array objectAtIndex:pageIndex];
        thvc.title = [secA objectAtIndex:pageIndex];
        [self.navigationController pushViewController:thvc animated:YES];
        [thvc release];
    };
   
    mtableview.tableHeaderView = mview;
    [mtableview addSubview:self.scroll];
    [self.view addSubview:mtableview];
    
    [_scroll release];
    [mview release];
    [mtableview release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    JournalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[JournalTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    JournalModel *model = [self.dataArray objectAtIndex:indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%@",model.thumbnail];
    NSURL *url = [NSURL URLWithString:str];
    [cell.miamgeView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Bei"]];
    cell.label.text = model.title;
    cell.label.numberOfLines = 0;
    [cell.label setFont:[UIFont systemFontOfSize:16]];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 230;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JournalModel *model =[self.dataArray objectAtIndex:indexPath.row];
    JournalSecondViewController *second = [[JournalSecondViewController alloc]init];
    second.self.str = model.post_link;
    [self.navigationController pushViewController:second animated:YES];
}
- (void)aa
{

}
-(NSArray *)getParserResult
{
    NSString *str = @"http://www.autobeta.cn/data/newcars.xml";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20];
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data  = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    parser.delegate = self;
    [parser parse];
    return 0;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"items"]) {
        self.dataArray = [NSMutableArray array];
    }else if ([elementName isEqualToString:@"title"])
    {
        JournalModel *model = [JournalModel getJournalModel];
        [self.dataArray addObject:model];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    self.tempStr = string;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    JournalModel *model = [self.dataArray lastObject];
    if ([elementName isEqualToString:@"title"]) {
        model.title = self.tempStr;
    }
    else if ([elementName isEqualToString:@"thumb600"])
    {
        model.thumbnail = self.tempStr;
    }else if ([elementName isEqualToString:@"pubDate"]){
        model.pubDate = self.tempStr;
    }
    else if([elementName isEqualToString:@"post_link"]){
        model.post_link = self.tempStr;
    }
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
